/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.tools {
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.display.ui.scroller.IScrollable;

	/**
	 * @author Marek Brun
	 */
	public class ScrolledRangeModel extends MLBroadcastClass implements IScrollable {
		
		public function get event_ScrollChanged():String { return 'ScrollChanged'; }
		
		private var scroll:Number;
		private var scrollAreaBegin:Number;
		private var scrollAreaEnd:Number;
		private var scrolledLength:Number;
		
		public function ScrolledRangeModel() {
			scroll=0;
		}
		
		public function setupScrollArea(scrollAreaBegin:Number, scrollAreaEnd:Number):void {
			if(this.scrollAreaBegin==scrollAreaBegin && this.scrollAreaEnd==scrollAreaEnd){ return; }
			this.scrollAreaBegin=scrollAreaBegin;
			this.scrollAreaEnd=scrollAreaEnd;
			broadcastEvent(event_ScrollChanged);
		}
		
		public function setupScrolledLength(scrolledLength:Number):void {
			this.scrolledLength=scrolledLength;
			broadcastEvent(event_ScrollChanged);
		}
		
		public function getScrolledBegin():Number {
			return scrollAreaBegin-scroll*(scrolledLength-(scrollAreaEnd-scrollAreaBegin));
		}
		
		public function getScroll():Number {
			return scroll;
		}
		
		public function getVisibleToTotal():Number {
			return (scrollAreaEnd-scrollAreaBegin)/scrolledLength;
		}
		
		public function setScroll(scroll:Number):void {
			this.scroll=Math.max(0, Math.min(1, scroll));
			broadcastEvent(event_ScrollChanged);
		}

		public function setScrollByOneUnit(isUp:Boolean):void {
			setScroll(isUp ? scroll+0.1 : scroll-0.1);
		}
	}
}
